// 网关配置的前缀
var jobsServer = "/jobsSer"

let userId = "1354255569179971599"

let educationList = "";//学历下拉框
let skillList = "";//技能掌握程度下拉框

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    let authorities = $("#authorities").html();
    unauthorized_2(authorities, "profile");

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        jobsServer = data.jobsServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    userId = $("#userId").html();
    $(".userId").val(userId);
    getEducation();
    getSkill();
    getExperience();


    getProfile();

}

// 获得 学历要求
function getEducation() {
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/educationLevel",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("学历要求");

            // 如果为空
            if (data.datas.length <= 0) {
                educationList += "<option value=''>暂无数据</option>"
            } else {
                //返回的数据用data.rows获取内容
                $.each(data.datas, function (i, item) {
                    educationList += "<option value='" + item.value + "'>" + item.value + "</option>";
                });
            }
        },
        error: function (err) {
            educationList += "<option value=''>暂无数据</option>"
        }

    });
}

// 获得 掌握技能
function getSkill() {
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/skillDegree",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("掌握技能");

            // 如果为空
            if (data.datas.length <= 0) {
                skillList += "<option value=''>暂无数据</option>"
            } else {
                //返回的数据用data.rows获取内容
                $.each(data.datas, function (i, item) {
                    skillList += "<option value='" + item.value + "'>" + item.value + "</option>";
                });
            }
        },
        error: function (err) {
            skillList += "<option value=''>暂无数据</option>"
        }

    });
}

// 获得 工作经验
function getExperience() {
    let $list = $("#experienceList .list");
    let $mySelect = $("#experienceList .mySelect");
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/experience",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("工作经验");

            // 如果为空
            if (data.datas.length <= 0) {
                $mySelect.empty();
                $mySelect.append(
                    "<option>暂无数据</option>"
                );
            }

            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                if (i === 0) {
                    $('#experienceList .current').html(item.value);
                }
                $list.append(
                    "<li data-value='" + item.value + "' class='option'>" + item.value + "</li>"
                );
            });
        },
        error: function (err) {
            $mySelect.empty();
            $mySelect.append(
                "<option>暂无数据</option>"
            );
        }

    });
}

// 获取 所有简历信息
function getProfile() {

    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/account/profile?userId=" + userId,
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("所有简历信息");

            let baseInfo = data.datas.baseInfo;
            if (baseInfo === null || baseInfo === ""){
                return;
            }
            if (baseInfo.photo !== "" && baseInfo.photo !== null){
                $(".create-photo-left").css("background-image", "url(" + baseInfo.photo + ")")
                    .css(" background-repeat", "no-repeat")
                    .css('background-size', '100% 100%');
                $("#photo-logo *").hide();
            }
            if (baseInfo.name !== null && baseInfo.name !== ""){
                $("#name").val(baseInfo.name);
            }
            if (baseInfo.birthday !== null && baseInfo.birthday !== ""){
                $("#birthday").val(baseInfo.birthday);
            }
            if (baseInfo.email !== null && baseInfo.email !== ""){
                $("#email").val(baseInfo.email);
            }
            if (baseInfo.phone !== null && baseInfo.phone !== ""){
                $("#phone").val(baseInfo.phone);
            }
            if (baseInfo.sex !== null && baseInfo.sex !== ""){
                $("input:radio[name='sex'][value="+baseInfo.sex+"]").attr('checked','true');
            }
            if (baseInfo.city !== null && baseInfo.city !== ""){
                $("#city").val(baseInfo.city);
            }
            if (baseInfo.industry !== null && baseInfo.industry !== ""){
                $("#type").val(baseInfo.industry);
            }
            if (baseInfo.salary !== null && baseInfo.salary !== ""){
                $("#money").val(baseInfo.salary);
            }
            if (baseInfo.selfEvaluation !== null && baseInfo.selfEvaluation !== ""){
                $("#your_message").html(baseInfo.selfEvaluation);
            }
            if (baseInfo.experience !== null && baseInfo.experience !== ""){
                $('#experienceList .current').html(baseInfo.experience);
            }

            $.each(data.datas.educations, function (i, item) {
                add_education(item);
            })

            $.each(data.datas.skills, function (i, item) {
                add_skills(item);
            })

            $.each(data.datas.works, function (i, item) {
                add_work(item);
            })

            $.each(data.datas.projects, function (i, item) {
                add_project(item);
            })

            $.each(data.datas.websites, function (i, item) {
                add_website(item);
            })


        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });

}


// 上传照片 部分
$("input[name='photo']").change(function (event) {
    // 获取图片元素
    var ele = event.target;

    if (!/.(gif|jpg|jpeg|png|GIF|JPG|bmp)$/.test(ele.value)) {
        alert("图片类型必须是.gif,jpeg,jpg,png,bmp中的一种");
        return false;
    }

    var files = ele.files;
    if (files && files.length <= 0) {
        alert("出错了，请重新上传。")
    }

    var file = files[0];
    if (file.size > 1024 * 100) {
        alert('照片大小不能超过 100KB!');
        return false;
    }

    // 获取目前上传的文件
    console.log("上传了照片");
    console.log(file);

    var path = URL.createObjectURL(file);
    $(".create-photo-left").css("background-image", "url(" + path + ")")
        .css(" background-repeat", "no-repeat")
        .css('background-size', '100% 100%');
    $("#photo-logo *").hide();


    // 将文件传到后端
    $('#uploadPhoto').ajaxSubmit({
        datatype: 'json',
        success: function (res) {
            cocoMessage.info(2000, "图片上传成功", function () {
            });
        },
        error: function (res) {
            cocoMessage.error(2000, "图片上传出错，请重试", function () {
            });
        }
    });

});

// 时间选择框，单选框
$(function () {
    $('.sex :input').labelauty();
});
$("#birthday").datetimepicker({
    minView: "month", //选择日期后，不会再跳转去选择时分秒
    format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
    language: 'zh-CN', //汉化
    autoclose: true //选择日期后自动关闭
});
$(".study-date").datetimepicker({
    format: 'yyyy-mm',
    language: 'zh-CN',
    todayBtn: true,
    startView: 4,
    maxView: 'decade',
    minView: 'year',
    forceParse: false,
    bootcssVer: 3,
    autoclose: true //选择日期后自动关闭
});
$(".work-date").datetimepicker({
    format: 'yyyy-mm-dd',
    language: 'zh-CN',
    todayBtn: true,
    startView: 4,
    maxView: 'decade',
    minView: 2,
    forceParse: false,
    bootcssVer: 3,
    autoclose: true //选择日期后自动关闭
});

// 追加模块部分
var education_index = 1;
var skills_index = 1;
var work_index = 1;
var project_index = 1;
var website_index = 1;

// 追加 教育经历 部分
function add_education(education) {
    let educationId = "";
    let school = "";
    let startDate = "";
    let endDate = "";
    let level = "";
    let major = "";
    let experience = "";

    if (education !== "" && education !== null) {
        educationId = education.educationId;
        school = education.school;
        startDate = education.startDate;
        endDate = education.endDate;
        level = education.level;
        major = education.major;
        experience = education.experience;
    }

    var model = "<form style='border-top: 1px solid gray;' id='del-education-" + education_index + "'>" +
        "<div class='row'>" +
        "<div class='col-lg-12'>" +
        "<div class='form-group'>" +
        "<label>教育经历 - " + education_index + "</label>" +
        "<input id='educationId-" + education_index + "' value='" + educationId + "' hidden/>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-6'>" +
        "<div class='form-group'>" +
        "<label>学校名称</label>" +
        "<input value='" + school + "' type='text' class='form-control' id='school-" + education_index + "' placeholder='学校'>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-6'>" +
        "<div class='form-group'>" +
        "<label>教育时间段</label>" +
        "<div>" +
        "<input value='" + startDate + "' id='start_date-" + education_index + "' type='text' class='study-date form-control col-lg-5' style='float: left; background: white;' placeholder='起始年月' readonly>" +
        "<input type='text' class='form-control col-lg-2' style='float: left; font-size: 30px;border: none;' readonly value=' -'>" +
        "<input value='" + endDate + "' id='end_date-" + education_index + "' type='text' class='study-date form-control col-lg-5' style='float: right; background: white;' placeholder='毕业年月' readonly>" +
        "</div>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-6'>" +
        "<div class='form-group'>" +
        "<label>学历</label>" +
        "<select class='form-control' id='education-" + education_index + "'>" +
        educationList +
        "</select>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-6'>" +
        "<div class='form-group'>" +
        "<label>专业</label>" +
        "<input value='" + major + "' type='text' class='form-control' id='major-" + education_index + "'>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-12'>" +
        "<div class='form-group'>" +
        "<label>在校经历</label>" +
        "<textarea id='association_activity-" + education_index + "' class='form-control' rows='3' placeholder='在校经历 ……'></textarea>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-12'>" +
        "<button class='btn btn-danger' style='float: right;' onclick='education(" + education_index + ", \"delete\");'>删除</button>" +
        "<button onclick='education(" + education_index + ", \"update\");return false;' class='btn btn-success' style='float: right; margin-right: 20px;' >保存</button>" +
        "</div>" +
        "</div>" +
        "</form>"

    $('#add-education').append(model);

    $("#association_activity-"+education_index).html(experience)
    $("#education-" + education_index + " option[value = '" + level + "']").attr("selected", true);

    education_index += 1;

    $(".study-date").datetimepicker({
        format: 'yyyy-mm',
        language: 'zh-CN',
        todayBtn: true,
        startView: 4,
        maxView: 'decade',
        minView: 'year',
        forceParse: false,
        bootcssVer: 3,
        autoclose: true //选择日期后自动关闭
    });
}

// 追加 掌握技能 部分
function add_skills(skill) {
    let skillId = "";
    let skillName = "";
    let degree = "";

    if (skill !== "" && skill !== null) {
        skillId = skill.skillId;
        skillName = skill.skillName;
        degree = skill.degree;
    }


    var model = "<form style='border-top: 1px solid gray;' id='del-skills-" + skills_index + "'>" +
        "<div class='row'>" +
        "<div class='col-lg-5'>" +
        "<div class='form-group'>" +
        "<label>技能 - " + skills_index + "</label>" +
        "<input id='skillId-" + skills_index + "' value='" + skillId + "' hidden/>" +
        "<input value='" + skillName + "' type='text' class='form-control' id='skills-" + skills_index + "' placeholder='掌握技能'>" +
        " </div>" +
        "</div>" +
        "<div class='col-lg-3'>" +
        "<div class='form-group'>" +
        "<label>掌握程度</label>" +
        "<select class='form-control' id='degree-" + skills_index + "'>" +
        "<option value='' selected=''>请选择</option>" +
        skillList +
        "</select>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-2'>" +
        "<div class='form-group' style='text-align: center;'>" +
        "<label>保存</label>" +
        "<a onclick='skill(" + skills_index + ", \"update\")' class='form-control lnr lnr-thumbs-up' style='border: none;line-height: 35px'></a>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-2'>" +
        "<div class='form-group' style='text-align: center;'>" +
        "<label>删除</label>" +
        "<a onclick='skill(" + skills_index + ", \"delete\")' class='form-control lnr lnr-trash' style='border: none;line-height: 35px'></a>" +
        "</div>" +
        "</div>" +
        "</div>" +
        "</form>"
    $('#add-skills').append(model);

    $("#degree-" + skills_index + " option[value = '" + degree + "']").attr("selected", true);

    skills_index += 1;
}

// 追加 工作经历 部分
function add_work(work) {
    let workId = "";
    let companyName = "";
    let industry = "";
    let dept = "";
    let position = "";
    let startDate = "";
    let endDate = "";
    let content = "";

    if (work !== "" && work !== null){
        workId = work.workId;
        companyName = work.companyName;
        industry = work.industry;
        startDate = work.startDate;
        endDate = work.endDate;
        content = work.content;

        if (work.dept !== null && work.dept !== ""){
            dept = work.dept;
        }
        if (work.position !== null && work.position !== ""){
            position = work.position;
        }
    }


    var model = "<form style='border-top: 1px solid gray;' id='del-work-" + work_index + "'>" +
        "<div class='row'>" +
        "<div class='col-lg-12'>" +
        "<div class='form-group'>" +
        "<label>工作经历 - " + work_index + "</label>" +
        "<input id='workId-" + work_index + "' value='" + workId + "' hidden/>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-6'>" +
        "<div class='form-group'>" +
        "<label>公司名称</label>" +
        "<input value='" + companyName + "' type='text' class='form-control' id='company-name-" + work_index + "' placeholder='例如：阿里巴巴网络技术有限公司'>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-6'>" +
        "<div class='form-group'>" +
        "<label>所属行业</label>" +
        "<input value='" + industry + "' type='text' class='form-control' id='industry-" + work_index + "' placeholder='行业所属'>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-6'>" +
        "<div class='form-group'>" +
        "<label>所属部门（选填）</label>" +
        "<input value='" + dept + "' type='text' class='form-control' id='department-" + work_index + "' placeholder='例如：产品部'>" +
        " </div>" +
        "</div>" +
        "<div class='col-lg-6'>" +
        "<div class='form-group'>" +
        "<label>职位名称（选填）</label>" +
        "<input value='" + position + "' type='text' class='form-control' id='position-" + work_index + "' placeholder='例如：UI 设计师'>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-8'>" +
        "<div class='form-group'>" +
        "<label>在职时间段</label>" +
        "<div>" +
        "<input value='" + startDate + "' id='work_start_date-" + work_index + "' type='text' class='work-date form-control col-lg-4' style='float: left; background: white;' placeholder='起始年月日' readonly>" +
        "<input type='text' class='form-control col-lg-2' style='float: left; font-size: 30px;border: none;' readonly value=' -'>" +
        "<input value='" + endDate + "' id='work_end_date-" + work_index + "' type='text' class='work-date form-control col-lg-4' style='float: left; background: white;' placeholder='结束年月日' readonly>" +
        "<div style='float: left;margin-top: 10px;' class='col-lg-2'>" +
        "<input onchange='zhiJin(\"work\","+project_index+")' type='checkbox' id='work-now-" + work_index + "' >" +
        "<label>至今</label>" +
        "</div>" +
        "</div>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-12'>" +
        "<div class='form-group'>" +
        "<label>工作内容</label>" +
        "<textarea id='work-content-" + work_index + "' class='form-control' rows='3' placeholder='工作内容 ……'></textarea>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-12'>" +
        "<button onclick='work(" + work_index + ", \"delete\");return false;' class='btn btn-danger' style='float: right;'>删除</button>" +
        "<button onclick='work(" + work_index + ", \"update\");return false;' class='btn btn-success' style='float: right; margin-right: 20px;'>保存</button>" +
        "</div>" +
        "</div>" +
        "</form>"
    $('#add-work').append(model);
    $("#work-content-"+work_index).html(content);
    if (endDate === "至今"){
        $('#work-now-'+work_index).prop('checked', true);
    }
    work_index += 1;
    $(".work-date").datetimepicker({
        format: 'yyyy-mm',
        language: 'zh-CN',
        todayBtn: true,
        startView: 4,
        maxView: 'decade',
        minView: 'year',
        forceParse: false,
        bootcssVer: 3,
        autoclose: true //选择日期后自动关闭
    });
}

// 追加 项目经历 部分
function add_project(project) {
    let projectId = "";
    let projectName = "";
    let role = "";
    let projectUrl = "";
    let startDate = "";
    let endDate = "";
    let content = "";

    if (project !== null && project !== ""){
        projectId = project.projectId;
        projectName = project.projectName;
        role = project.role;
        startDate = project.startDate;
        endDate = project.endDate;
        if (project.projectUrl !== "" && project.projectUrl !== null){
            projectUrl = project.projectUrl;
        }
        if (project.content !== "" && project.content !== null){
            content = project.content;
        }
    }


    var model = "<form style='border-top: 1px solid gray;' id='del-project-" + project_index + "'>" +
        "<div class='row'>" +
        "<div class='col-lg-12'>" +
        "<div class='form-group'>" +
        "<label>项目经历 - " + project_index + "</label>" +
        "<input id='projectId-" + project_index + "' value='" + projectId + "' hidden/>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-6'>" +
        "<div class='form-group'>" +
        "<label>项目名称</label>" +
        "<input value='" + projectName + "' type='text' class='form-control' id='project-name-" + project_index + "' placeholder='项目名称'>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-6'>" +
        "<div class='form-group'>" +
        "<label>项目角色</label>" +
        "<input value='" + role + "' type='text' class='form-control' id='project-role-" + project_index + "' placeholder='例如：UI 设计师'>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-12'>" +
        "<div class='form-group'>" +
        "<label>项目链接(选填)</label>" +
        "<input value='" + projectUrl + "' type='text' class='form-control' id='project-url-" + project_index + "' placeholder='例如：https://gitee.com/yuan934672344/my-job'>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-8'>" +
        "<div class='form-group'>" +
        "<label>项目起始时间</label>" +
        "<div>" +
        "<input value='" + startDate + "' id='pro_start_date-" + project_index + "' type='text' class='study-date form-control col-lg-4' style='float: left; background: white;' placeholder='起始年月' readonly>" +
        "<input type='text' class='form-control col-lg-2' style='float: left; font-size: 30px;border: none;' readonly value='  -'>" +
        "<input value='" + endDate + "' id='pro_end_date-" + project_index + "' type='text' class='study-date form-control col-lg-4' style='float: left; background: white;' placeholder='结束年月' readonly>" +
        "<div style='float: left;margin-top: 10px;' class='col-lg-2'>" +
        "<input onchange='zhiJin(\"pro\","+project_index+")' type='checkbox' id='pro_now-" + project_index + "'>" +
        "<label>至今</label>" +
        "</div>" +
        "</div>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-12'>" +
        "<div class='form-group'>" +
        "<label>项目描述</label>" +
        "<textarea id='pro-content-" + project_index + "' class='form-control' rows='3' placeholder='工作内容 ……'></textarea>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-12'>" +
        "<button onclick='project(" + project_index + ", \"delete\");return false;' class='btn btn-danger' style='float: right;'>删除</button>" +

        "<button onclick='project(" + project_index + ", \"update\");return false;' class='btn btn-success' style='float: right; margin-right: 20px;'>保存</button>" +
        "</div>" +
        "</div>" +
        "</form>"
    $('#add-project').append(model);
    $("#pro-content-"+project_index).html(content);
    if (endDate === "至今"){
        $('#pro_now-'+project_index).prop('checked', true);
    }
    project_index += 1;
    $(".study-date").datetimepicker({
        format: 'yyyy-mm',
        language: 'zh-CN',
        todayBtn: true,
        startView: 4,
        maxView: 'decade',
        minView: 'year',
        forceParse: false,
        bootcssVer: 3,
        autoclose: true //选择日期后自动关闭
    });
}

// 追加 个人网站 部分
function add_website(website) {
    let websiteId = "";
    let websiteUrl = "";
    if (website !== null && website !== ""){
        websiteId = website.websiteId;
        websiteUrl = website.websiteUrl;
    }

    var model = "<form style='border-top: 1px solid gray;' id='del-website-" + website_index + "'>" +
        "<div class='row'>" +
        "<div class='col-lg-8'>" +
        "<div class='form-group'>" +
        "<label>个人网站 - " + website_index + "</label>" +
        "<input id='websiteId-" + website_index + "' value='" + websiteId + "' hidden/>" +
        "<input value='" + websiteUrl + "' type='text' class='form-control' id='personal-website-" + website_index + "' placeholder='CSDN、GitHub ……'>" +
        "</div>" +
        "</div>" +
        "<div class='col-lg-2'>" +
        "<div class='form-group' style='text-align: center;'>" +
        "<label>保存</label>" +
        "<a onclick='website(" + website_index + ", \"update\");return false;' class='form-control lnr lnr-thumbs-up' style='border: none;line-height: 35px'></a>" +
        " </div>" +
        "</div>" +
        "<div class='col-lg-2'>" +
        "<div class='form-group' style='text-align: center;'>" +
        "<label>删除</label>" +
        "<a onclick='website(" + website_index + ", \"delete\");return false;' class='form-control lnr lnr-trash' style='border: none;line-height: 35px'></a>" +
        "</div>" +
        "</div>" +
        "</div>" +
        "</form>"
    website_index += 1;
    $('#add-website').append(model);
}

// 移除模块部分
function del_model(del_id, del_num) {
    $('#' + del_id + '-' + del_num).remove();
    console.log(del_id + '-' + del_num + '被删除了');
}

function baseInfo(){
    let birthday = $("#birthday").val();
    let sex = $('input[name="sex"]:checked').val();
    let city = $("#city").val();
    let industry = $("#type").val();
    let name = $("#name").val();
    let phone = $("#phone").val();
    let email = $("#email").val();
    let salary = $("#money").val();
    let experience = $('#experienceList .current').html();
    let selfEvaluation = $("#your_message").html();

    $.ajax({
        type: "POST",
        async: false,
        url: jobsServer + "/account/baseInfo",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "userId": userId,
            "birthday": birthday,
            "sex": sex,
            "city": city,
            "industry": industry,
            "name": name,
            "phone": phone,
            "email": email,
            "salary": salary,
            "experience": experience,
            "selfEvaluation": selfEvaluation,
        }),
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg, function () {
                });
            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

function education(index, option) {

    let educationId = $("#educationId-" + index).val();
    let school = $("#school-" + index).val();
    let startDate = $("#start_date-" + index).val();
    let endDate = $("#end_date-" + index).val();
    let level = $("#education-" + index).selected().val();
    let major = $("#major-" + index).val();
    let experience = $("#association_activity-" + index).val();

    let type = "POST";

    if (educationId === null ||
        educationId === "") {
        if (option === "delete") {
            del_model("del-education", index);
            education_index -= 1;
            return;
        } else {
            type = "PUT";
        }
    } else {
        if (option === "delete") {
            type = "DELETE";
        }
    }

    $.ajax({
        type: type,
        async: false,
        url: jobsServer + "/account/education",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "educationId": educationId,
            "userId": userId,
            "school": school,
            "startDate": startDate,
            "endDate": endDate,
            "level": level,
            "major": major,
            "experience": experience,
        }),
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg, function () {
                });

                if (option === "delete") {
                    location.reload();
                }

            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

function skill(index, option) {
    let skillId = $("#skillId-" + index).val();
    let skillName = $("#skills-" + index).val();
    let degree = $("#degree-" + index).selected().val();

    let type = "POST";

    if (skillId === null ||
        skillId === "") {
        if (option === "delete") {
            del_model("del-skills", index);
            skills_index -= 1
            return;
        } else {
            type = "PUT";
        }
    } else {
        if (option === "delete") {
            type = "DELETE";
        }
    }

    $.ajax({
        type: type,
        async: false,
        url: jobsServer + "/account/skill",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "skillId": skillId,
            "userId": userId,
            "skillName": skillName,
            "degree": degree,
        }),
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg, function () {
                });

                if (option === "delete") {
                    location.reload();
                }

            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })

}

function work(index, option) {
    let workId = $("#workId-" + index).val();
    let companyName = $("#company-name-" + index).val();
    let industry = $("#industry-" + index).val();
    let dept = $("#department-" + index).val();
    let position = $("#position-" + index).val();
    let startDate = $("#work_start_date-" + index).val();
    let endDate = $("#work_end_date-" + index).val();
    let content = $("#work-content-" + index).val();

    let type = "POST";

    if (workId === null ||
        workId === "") {
        if (option === "delete") {
            del_model("del-work", index);
            work_index-=1
            return;
        } else {
            type = "PUT";
        }
    } else {
        if (option === "delete") {
            type = "DELETE";
        }
    }

    $.ajax({
        type: type,
        async: false,
        url: jobsServer + "/account/work",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "userId": userId,
            "workId": workId,
            "companyName": companyName,
            "industry": industry,
            "dept": dept,
            "position": position,
            "startDate": startDate,
            "endDate": endDate,
            "content": content,
        }),
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg, function () {
                });

                if (option === "delete") {
                    location.reload();
                }

            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })

}

function project(index, option) {
    let projectId = $("#projectId-" + index).val();
    let projectName = $("#project-name-" + index).val();
    let role = $("#project-role-" + index).val();
    let projectUrl = $("#project-url-" + index).val();
    let startDate = $("#pro_start_date-" + index).val();
    let endDate = $("#pro_end_date-" + index).val();
    let content = $("#pro-content-" + index).val();

    let type = "POST";

    if (projectId === null ||
        projectId === "") {
        if (option === "delete") {
            del_model("del-project", index);
            project_index-=1;
            return;
        } else {
            type = "PUT";
        }
    } else {
        if (option === "delete") {
            type = "DELETE";
        }
    }

    $.ajax({
        type: type,
        async: false,
        url: jobsServer + "/account/project",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "userId": userId,
            "projectId": projectId,
            "projectName": projectName,
            "role": role,
            "projectUrl": projectUrl,
            "startDate": startDate,
            "endDate": endDate,
            "content": content,
        }),
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg, function () {
                });

                if (option === "delete") {
                    location.reload();
                }

            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })

}

function website(index, option) {
    let websiteId = $("#websiteId-" + index).val();
    let websiteUrl = $("#personal-website-" + index).val();

    let type = "POST";

    if (websiteId === null ||
        websiteId === "") {
        if (option === "delete") {
            del_model("del-website", index);
            website_index-=1
            return;
        } else {
            type = "PUT";
        }
    } else {
        if (option === "delete") {
            type = "DELETE";
        }
    }

    $.ajax({
        type: type,
        async: false,
        url: jobsServer + "/account/website",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "userId": userId,
            "websiteId": websiteId,
            "websiteUrl": websiteUrl,
        }),
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg, function () {
                });

                if (option === "delete") {
                    location.reload();
                }

            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })

}

function zhiJin(prefix, index){
    let $zhiJin = $('#'+prefix+"_now-"+index);
    let $endDate = $('#'+prefix+"_end_date-"+index);
    let isChecked = $zhiJin.prop('checked');
    if (isChecked){
        $endDate.val("至今");
        $endDate.attr("readOnly");
    }else {
        $endDate.val("");
        $endDate.removeAttr("readOnly");
    }
}
